#!/bin/bash
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

function rc_status () {
	[[ $# -ne 1 ]] && return 1

	local _svc="$1"

	"/etc/init.d/${_svc}" -sCq status > /dev/null 2>&1
}

function chk-htdocs () {
	local _root="$(realpath ${VHOST_ROOT})"
	local _htdocs="$(realpath ${VHOST_HTDOCSDIR})"

	_root="${_root//[^\/]}"
	_htdocs="${_htdocs//[^\/]}"

	if (( ${#_htdocs} - ${#_root} != 1 )) ; then
		echo "VHOST_HTDOCSDIR MUST be one level subdirectory of VHOST_ROOT"
		echo ""
		echo "What you have is"
		echo "	VHOST_ROOT=${VHOST_ROOT}"
		echo "	VHOST_HTDOCSDIR=${VHOST_HTDOCSDIR}"
		echo ""
		echo "Example of what you should have"
		echo "	VHOST_ROOT=/var/www/localhost"
		echo "	VHOST_HTDOCSDIR=/var/www/localhost/public_html"
		echo ""
		echo "You must fix your webapp-config configuration"
		exit 1
	fi

	return 0
}

function fix-perms () {
	echo "Making executable all shebanged files"
	find "${VHOST_ROOT}" -type f \
		| xargs -n 1 --no-run-if-empty -- \
			awk 'NR == 1 && /^#!/ {print FILENAME}' \
		| xargs --no-run-if-empty -- \
			chmod 755

	return 0
}

function fix-webroot () {
	# Abort if vhost_htdocs is already webroot
	[[ "${VHOST_HTDOCSDIR/#${VHOST_ROOT}\/}" == "webroot" ]] \
		&& return 0

	# Make sur VHOST_ROOT/webroot is a symlink
	# Otherwise backup it
	if [[ -e "${VHOST_ROOT}/webroot" && ! -L "${VHOST_ROOT}/webroot" ]] ; then
		echo "Backing up ${VHOST_ROOT}/webroot to ${VHOST_ROOT}/webroot.orig"
		mv "${VHOST_ROOT}/webroot" "${VHOST_ROOT}/webroot.orig"
	else
		rm -f "${VHOST_ROOT}/webroot"
	fi

	echo "Creating symlink ${VHOST_ROOT}/webroot"
	ln -s "${VHOST_HTDOCSDIR}" "${VHOST_ROOT}/webroot"

	echo "Changing ${VHOST_ROOT}/webroot owner to ${VHOST_SERVER_UID}:${VHOST_SERVER_GID}"
	chown -h "${VHOST_SERVER_UID}:${VHOST_SERVER_GID}" "${VHOST_ROOT}/webroot"

	return 0
}

function storage-upgrade () {
	local _date="$(date +'%F-%T')"

	local _has_aphlictd=no
	local _has_phd=no
	local _has_server=no

	# Do not upgrade anything if we can't connect to databases
	if ! "${VHOST_ROOT}/bin/storage" databases > /dev/null ; then
		echo "Aborting, connection to database unsuccessfull"
		echo "You need to manually run"
		echo "	${VHOST_ROOT}/bin/storage upgrade"
		return 1
	fi

	case "${VHOST_SERVER}" in
		apache)	VHOST_SERVER="apache2" ;;
	esac

	# Save service status
	rc_status aphlictd          && _has_aphlictd=yes
	rc_status phd               && _has_phd=yes
	rc_status "${VHOST_SERVER}" && _has_server=yes

	# Only stop started services
	[[ "${_has_aphlictd:-no}" == "yes" ]] && /etc/init.d/aphlictd          stop
	[[ "${_has_phd:-no}"      == "yes" ]] && /etc/init.d/phd               stop
	[[ "${_has_server:-no}"   == "yes" ]] && /etc/init.d/"${VHOST_SERVER}" stop

	echo "Backing up database to ${VHOST_ROOT}/backup/dump-${_date}.sql.xz"
	[[ ! -d "${VHOST_ROOT}/backup" ]] \
		&& mkdir -m 0750 "${VHOST_ROOT}/backup"

	echo "  ${VHOST_ROOT}/bin/storage dump"
	"${VHOST_ROOT}/bin/storage" dump \
		| xz -v > "${VHOST_ROOT}/backup/dump-${_date}.sql.xz"

	echo "Upgrading storage"
	echo "  ${VHOST_ROOT}/bin/storage upgrade"
	"${VHOST_ROOT}/bin/storage" upgrade \
		|| echo "failed, you should analyze it and rerun manually" ; exit 1

	# Only start services that was previously started
	[[ "${_has_server:-no}"   == "yes" ]] && /etc/init.d/"${VHOST_SERVER}" start
	[[ "${_has_phd:-no}"      == "yes" ]] && /etc/init.d/phd               start
	[[ "${_has_aphlictd:-no}" == "yes" ]] && /etc/init.d/aphlictd          start

	return 0
}

[[ $# -ne 1 ]] && exit 1

case "$1" in
	install)
		echo ""
		chk-htdocs
		fix-perms
		fix-webroot
		storage-upgrade
		echo ""
		;;
esac
